package eteam.aps.systemservice.service;

import cn.hutool.core.convert.Convert;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import eteam.aps.common.QueryResult;
import eteam.aps.common.R;
import eteam.aps.common.exceptions.AuthException;
import eteam.aps.systeminterface.model.dto.UserDto;
import eteam.aps.systeminterface.model.dto.VerifyTokenDto;
import eteam.aps.systeminterface.model.entity.OnlineUserEntity;
import eteam.aps.systeminterface.model.entity.UserEntity;
import eteam.aps.systeminterface.model.enums.StateEnum;
import eteam.aps.systeminterface.model.reqparam.*;
import eteam.aps.systeminterface.service.IUserController;
import eteam.aps.systemservice.bll.UserBll;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户服务
 */
@Api(tags = "系统服务控制器")
@RestController
//@RequestMapping(value = "/api/user")
public class UserController implements IUserController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserBll userBll;

    /**
     * 分页查询用户列表
     *
     * @param requestDto: 查询参数
     * @return
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "分页查询用户列表")
    @Override
    @GetMapping(value = "/api/user/querypagelist")
    public R<QueryResult<UserEntity>> queryUserPageList(@Validated QueryUserParam requestDto) throws Exception, AuthException {
        return R.success(userBll.queryUserPageList(requestDto));
    }

    /**
     * 新增用户
     *
     * @param userDto: 用户信息
     * @return
     */
    @ApiOperationSupport(order = 2, ignoreParameters = {"userDto.fuserid"})
    @ApiOperation(value = "新增用户")
    @Override
    @PostMapping(value = "/api/user/createuser")
    public R createUser(@Validated(Insert.class) @RequestBody UserDto userDto) throws Exception, AuthException {
        VerifyTokenDto tokenDto = Convert.convert(VerifyTokenDto.class, request.getAttribute("tokenInfo"));
        userBll.createUser(tokenDto, userDto);
        return R.success();
    }

    /**
     * 更新用户
     *
     * @param userDto: 用户信息
     * @return
     */
    @ApiOperationSupport(order = 3, ignoreParameters = {})
    @ApiOperation(value = "更新用户")
    @Override
    @PostMapping(value = "/api/user/updateuser")
    public R updateUser(@Validated(Update.class) @RequestBody UserDto userDto) throws Exception, AuthException {
        VerifyTokenDto tokenDto = Convert.convert(VerifyTokenDto.class, request.getAttribute("tokenInfo"));
        userBll.updateUser(tokenDto, userDto);
        return R.success();
    }

    /**
     * 删除用户
     *
     * @param userParam: 用户Id参数
     * @return
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "删除用户")
    @Override
    @PostMapping(value = "/api/user/deleteuser")
    public R deleteUser(@Validated @RequestBody IdParam userParam) throws Exception, AuthException {
        userBll.deleteUser(userParam.getObjid());
        return R.success();
    }

    /**
     * 变更状态，启用/禁用
     *
     * @param changeParam: 变更状态参数
     * @return
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "变更用户状态")
    @Override
    @PostMapping(value = "/api/user/changeuserstate")
    public R changeUserState(@Validated @RequestBody ChangeStateParam changeParam) throws Exception, AuthException {
        userBll.changeState(changeParam.getObjid(), StateEnum.values()[changeParam.getState()]);
        return R.success();
    }

    /**
     * 分页查询在线用户
     *
     * @param requestDto
     * @return
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "分页查询在线用户")
    @Override
    @GetMapping(value = "/api/user/queryonlineuserpagelist")
    public R<QueryResult<OnlineUserEntity>> queryOnlineUserPageList(@Validated QueryOnlineUserParam requestDto) throws Exception, AuthException {
        return R.success(userBll.queryOnlineUserPageList(requestDto));
    }

    /**
     * 删除在线用户，下线
     *
     * @param idParam
     * @return
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "删除在线用户，下线")
    @Override
    @PostMapping(value = "/api/user/deleteonlineuserbyid")
    public R deleteOnlineUserById(@Validated @RequestBody IdParam idParam) throws Exception, AuthException {
        userBll.deleteOnlineUser(idParam.getObjid());
        return R.success();
    }

    /**
     * 批量删除在线用户，下线
     *
     * @param idsParam
     * @return
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "批量删除在线用户，下线")
    @Override
    @PostMapping(value = "/api/user/deleteonlineuserbyids")
    public R deleteOnlineUserByIds(@Validated @RequestBody IdsParam idsParam) throws Exception, AuthException {
        userBll.deleteOnlineUser(idsParam.getObjids());
        return R.success();
    }

    /**
     * 重置密码
     *
     * @param resetPwdParam
     * @return
     */
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "重置密码")
    @Override
    @PostMapping(value = "/api/user/resetuserpwd")
    public R resetUserPwd(@Validated @RequestBody ResetPwdParam resetPwdParam) throws Exception, AuthException {
        userBll.resetUserPwd(resetPwdParam.getUserid(), resetPwdParam.getNewpwd());
        return R.success();
    }

    /**
     * 修改个人密码
     *
     * @param modifyPwdParam
     * @return
     */
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "修改个人密码")
    @Override
    @PostMapping(value = "/api/user/modifyselfpwd")
    public R modifySelfPwd(@Validated @RequestBody ModifySelfPwdParam modifyPwdParam) throws Exception, AuthException {
        VerifyTokenDto tokenDto = Convert.convert(VerifyTokenDto.class, request.getAttribute("tokenInfo"));
        userBll.modifyUserPwd(tokenDto.getFuserid(), modifyPwdParam.getOldpwd(), modifyPwdParam.getNewpwd());
        return R.success();
    }


    /**
     * 修改个人密码
     *
     * @param findPwdParam
     * @return
     */
    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "找回个人密码")
    @Override
    @PostMapping(value = "/api/user/findselfpwd")
    public R findSelPwd(@Validated @RequestBody FindPwdParam findPwdParam) throws Exception {
        return R.success(userBll.findSelPwd(findPwdParam));
    }

}
